package com.example.devicemanage.controller;

import com.example.devicemanage.entity.Break;
import com.example.devicemanage.entity.Repair;
import com.example.devicemanage.service.inter.BreakService;
import com.example.devicemanage.service.inter.RepairService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class BreakController {
    @Autowired
    BreakService breakService;
    @Autowired
    EquipController equipController;

    @ResponseBody
    @RequestMapping(path = "/insertBreak", method = RequestMethod.POST)
        //获取请求参数方式3,直接使用原生的HttpServletRequest
    String insertRepair(HttpServletRequest httpServletRequest) {
        String eid = httpServletRequest.getParameter("eid");
        System.out.println(eid);


        equipController.updateEquipstateThree(eid);


        String date = httpServletRequest.getParameter("date");

        String note = httpServletRequest.getParameter("note");

        Break b = new Break();
        b.setEid(eid);
        b.setDate(date);
        b.setNote(note);


        Boolean aBoolean = breakService.saveBreak(b);

//     return equipController.queryAllequip(httpServletRequest);
        if (aBoolean)
            return "OK";
        else return "something wrong!";


    }
    @ResponseBody
    @RequestMapping(path = "/break_update", method = RequestMethod.POST)
        //获取请求参数方式3,直接使用原生的HttpServletRequest
    String updateRepair(HttpServletRequest httpServletRequest) {
        String eid = httpServletRequest.getParameter("eid");
        System.out.println(eid);




        String bid = httpServletRequest.getParameter("bid");
        String date = httpServletRequest.getParameter("date");

        String note = httpServletRequest.getParameter("note");

        Break b = new Break();
        b.setBid(Integer.parseInt(bid));
        b.setEid(eid);
        b.setDate(date);
        b.setNote(note);


        Boolean aBoolean = breakService.updateBreak(b);

//     return equipController.queryAllequip(httpServletRequest);
        if (aBoolean)
            return "OK";
        else return "something wrong!";


    }

    @RequestMapping(path = "/break_list", method = RequestMethod.GET)
    public String getBreak_list(Model model) {
        List<Break> list =breakService.showAllBreak();
        model.addAttribute(list);
        return "break_list";
    }
    @ResponseBody
    @RequestMapping(path = "/break_delete")
    public String getRepair_delete(int bid) {


        return breakService.deleteByid(bid) == 1 ? "finished" : "wrong";

    }

}
